<?php

namespace BookSpider;

use BookSpider\SimpleHtmlDom;

define('DEFAULT_TARGET_CHARSET', 'UTF-8');
define('DEFAULT_BR_TEXT', "\r\n");
define('DEFAULT_SPAN_TEXT', " ");

/**
 * Spider Class
 *
 * @package		Spider
 * @subpackage	Libraries
 * @author		Sanli
 * @time		2016/04/11 22:20
 */
class Spider {

    public function __construct() {
        
    }

    /**
     * check encoding
     *
     * @param $text
     * @return string
     */
    public function checkEncoding($text) {
        return mb_detect_encoding($text, array('ASCII','UTF-8','GB2312','GBK','BIG5'));
    }

    /**
     * Convert encoding
     *
     * @param string $text
     * @return string
     */
    public function toUtf8($text) {
        $encode = $this->checkEncoding($text);
        //$encode = $encode == 'EUC-CN' ? "GBK" : $encode;
        return $encode == 'UTF-8' ? $text : iconv($encode, "UTF-8//IGNORE", $text);
    }

    /**
     * get middle string
     *
     * @param $text
     * @param $start
     * @param $end
     * @return string
     */
    public function getMiddleText($text, $start, $end) {

        $ret = '';
        $w = strpos($text, $start);
        if($w > -1) {
            $e = strpos($text, $end, $w);
            $ret = substr($text, $w + strlen($start), $e - $w - strlen($start));
        }
        return $ret;
    }
    
    public function match($htm, $rule, $filter = false) {
        
    }

    /**
     * 解析URL参数
     *
     * @param $url
     * @param $params
     * @return mixed
     */
    public function parseUrl($url, $params) {
        $re = '/(?<vars>\{[a-zA-z]*\})/';
        preg_match_all($re, $url, $result);
        if(isset($result['vars'])) {
            return str_replace($result['vars'], $params, $url);
        }
        return $url;
    }

    public function fileGetHtml($url, $use_include_path = false, $context=null, $offset = -1, $maxLen=-1, $lowercase = true, $forceTagsClosed=true, $target_charset = DEFAULT_TARGET_CHARSET, $stripRN=true, $defaultBRText=DEFAULT_BR_TEXT, $defaultSpanText=DEFAULT_SPAN_TEXT)
    {
        // We DO force the tags to be terminated.
        $dom = new SimpleHtmlDom(null, $lowercase, $forceTagsClosed, $target_charset, $stripRN, $defaultBRText, $defaultSpanText);
        // For sourceforge users: uncomment the next line and comment the retreive_url_contents line 2 lines down if it is not already done.
        $contents = file_get_contents($url, $use_include_path, $context, $offset);
        // Paperg - use our own mechanism for getting the contents as we want to control the timeout.
        //$contents = retrieve_url_contents($url);
        if (empty($contents) || strlen($contents) > MAX_FILE_SIZE)
        {
            return false;
        }
        // The second parameter can force the selectors to all be lowercase.
        $dom->load($contents, $lowercase, $stripRN);
        return $dom;
    }

    // get html dom from string
    function strGetHtml($str, $lowercase=true, $forceTagsClosed=true, $target_charset = DEFAULT_TARGET_CHARSET, $stripRN=true, $defaultBRText=DEFAULT_BR_TEXT, $defaultSpanText=DEFAULT_SPAN_TEXT)
    {
        $dom = new SimpleHtmlDom(null, $lowercase, $forceTagsClosed, $target_charset, $stripRN, $defaultBRText, $defaultSpanText);
        if (empty($str) || strlen($str) > MAX_FILE_SIZE)
        {
            $dom->clear();
            return false;
        }
        $dom->load($str, $lowercase, $stripRN);
        return $dom;
    }

    // dump html dom tree
    function dump_html_tree($node, $show_attr=true, $deep=0)
    {
        $node->dump($node);
    }
}
